package com.manor.paradise.activity

import android.graphics.Color
import android.text.SpannableString
import android.text.Spanned
import android.text.style.ForegroundColorSpan
import android.util.Log
import android.view.View
import android.widget.ImageView
import android.widget.TextView
import androidx.core.content.ContextCompat
import com.alibaba.fastjson.JSON
import com.blankj.utilcode.util.ToastUtils
import com.gyf.immersionbar.ImmersionBar
import com.lxj.xpopup.XPopup
import com.lxj.xpopup.enums.PopupAnimation
import com.manor.common.activity.AbsActivity
import com.manor.common.http.JHttpCallback
import com.manor.common.ktx.toBean
import com.manor.common.utils.*
import com.manor.paradise.R
import com.manor.paradise.bean.*
import com.manor.paradise.dialog.HireElvesPopup
import com.manor.paradise.dialog.LybInputPasswordDialog
import com.manor.paradise.http.ElvesHttpUtil
import com.opensource.svgaplayer.SVGAImageView
import kotlinx.android.synthetic.main.activity_feed_more_animals.*
import kotlinx.android.synthetic.main.activity_mine.*
import kotlinx.android.synthetic.main.dialog_hire_elves_popup.view.*
import kotlinx.android.synthetic.main.item_paradise_already_feed.view.*
import org.greenrobot.eventbus.EventBus
import org.greenrobot.eventbus.Subscribe
import org.greenrobot.eventbus.ThreadMode
import java.math.BigDecimal
import java.util.ArrayList
import kotlin.math.log

class LybFeedMoreAnimalsActivity : AbsActivity(), LybInputPasswordDialog.OnInputEndListener {
    override fun getLayoutId() = R.layout.activity_feed_more_animals

    override fun main() {
        ImmersionBar.with(this)
                .statusBarColor(R.color.transparent)
                .titleBar(tagLayout5)
                .statusBarDarkFont(false)
                .init()
        setClick(mCloseIv, mJzXiongLayout, mJzJiLayout, mJzGouLayout, mJzZhuLayout, mJzYangLayout, mJzNiuLayout, mJzHuLayout, mJzFengLayout, mJzLongLayout)
        initData()
    }

    private fun initData() {
        ElvesHttpUtil.getSpiritCount(object : JHttpCallback() {
            override fun onSuccess(code: Int, msg: String, info: String) {
                if (code != 200) {
                    ToastUtil.show(msg)
                    return
                }
                if (StringUtil.isEmpty(info))
                    return
                var bean = info.toBean<SpiritBean>()
                mMyRiseCountTv?.text = "我的米粒 ${bean.crystalOre}"
                mMyPowerTv?.text = "我有能量 ${bean.crystal}"
                val list: List<SpiritCount> = bean.spiritCounts
                setSCount(list, listOf(mJzXiongCountTv, mJzJiCountTv, mJzGouCountTv, mJzZhuCountTv, mJzYangCountTv, mJzNiuCountTv, mJzHuCountTv, mJzFengCountTv, mJzLongCountTv))

            }

            override fun onError() {
                super.onError()

            }
        })
    }

    private fun setSCount(list: List<SpiritCount>, tvArr: List<TextView?>) {
        for (i in list.indices) {
            list.findLast { it.spiritId == (i + 1).toString() }?.apply {
                tvArr[i]?.text = "${spiritCount}/${sameNum}"
                tvArr[i]?.visibility = View.VISIBLE
//                val p = if (i == 0) "免费" else price
//                tvArr[i]?.text = "${spiritCount}/${sameNum}  $p"
//                tvArr[i]?.visibility = View.VISIBLE
            }
        }
    }

    override fun onClick(v: View?) {
        super.onClick(v)
        if (v?.id == R.id.mCloseIv){
            finish()
        }else{
            v?.let {
                setPopDialog(it)
            }
        }
    }
    var type = true
    private fun setPopDialog(view: View) {
        if (type) {
            type = false
            showWaitingDialog(false)
            ElvesHttpUtil.getMineralInfo(BigDecimal(view.tag.toString()), object : JHttpCallback() {
                override fun onSuccess(code: Int, msg: String, info: String) {
                    dismissWaitingDialog()
                    if (code != 200) {
                        ToastUtil.show(msg)
                        return
                    }

                    type = true
                    val mineInfoBean: MineInfoBean = GsonUtil.gsonToBean<MineInfoBean>(info, MineInfoBean::class.java)
                    when(mineInfoBean.id.toInt()){
                        1 ->{//乌骨鸡-》胖胖熊
                            mineInfoBean.name = "胖胖熊 能量卷轴(赠送)"
                        }
                        2 ->{//茶花鸡-》咕咕鸡
                            mineInfoBean.name = "咕咕鸡 勤奋卷轴"
                        }
                        3 ->{//贵妃鸡-》汪汪狗
                            mineInfoBean.name = "汪汪狗 忠诚卷轴"
                        }
                        4 ->{//元宝鸡-》哼哼猪
                            mineInfoBean.name = "哼哼猪 富贵卷轴"
                        }
                        5 ->{//五黑鸡-》咩咩羊
                            mineInfoBean.name = "咩咩羊 吉祥卷轴"
                        }
                        6 ->{//三黄鸡-》哞哞牛
                            mineInfoBean.name = "哞哞牛 健康卷轴"
                        }
                        7 ->{//芦花鸡-》乐乐虎
                            mineInfoBean.name = "乐乐虎 成功卷轴"
                        }
                        8 ->{//珍珠鸡-》淘淘凤
                            mineInfoBean.name = "淘淘凤 进取卷轴"
                        }
                        9 ->{//天山雪鸡-》米米龙
                            mineInfoBean.name = "米米龙 尊贵卷轴"
                        }
                    }
                    LybInputPasswordDialog(this@LybFeedMoreAnimalsActivity, mineInfoBean).show(supportFragmentManager, "password")
                }

                override fun onError() {
                    super.onError()
                    dismissWaitingDialog()
                    type = true
                }
            })
        }

    }
    override fun end(password: String, entity: MineInfoBean, payType: Boolean) {
        if (entity.price.compareTo(BigDecimal("0")) == 0) {
            ElvesHttpUtil.getHireReward(password, object : JHttpCallback() {
                override fun onSuccess(code: Int, msg: String, info: String) {
                    ToastUtil.show(msg)
                    if (code == 200) {
                        SpUtil.getInstance().setBooleanValue(SpUtil.FRIRYLAND_BGETSPIRIT, true)
                        initData()
                    }
                }
            })
        }else{
            ElvesHttpUtil.getHire(entity.id, password, payType, object : JHttpCallback() {
                override fun onSuccess(code: Int, msg: String, info: String) {
                    ToastUtil.show(msg)
                    if (code == 200) {
                        initData()
                    }
                }
            })
        }
    }
}